package de.uni_stuttgart.ipvs.ids.rmi;

import java.io.IOException;
import java.net.MalformedURLException;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.Collection;

import de.uni_stuttgart.ipvs.ids.summation.ISummationSession;
import de.uni_stuttgart.ipvs.ids.summation.ISummationSessionFactory;

/**
 * Client for using an RMI summation service.
 * @author Team 3/4 - Elkhan Dadashov, Dhaval A Shah, Patrick Stortz
 */
public class RmiClient {
	
	/** The remote session factory to be used */
	ISummationSessionFactory mSessionFactory;
	
	/**
	 * Creates a RmiClient. Establishes the connection to the remote service.
	 * @param url
	 * @throws MalformedURLException
	 * @throws RemoteException
	 * @throws NotBoundException
	 */
	public RmiClient(String url) throws MalformedURLException, RemoteException, NotBoundException {
		// get the remote session factory
		mSessionFactory = (ISummationSessionFactory) java.rmi.Naming.lookup(url);
	}

	/**
	 * @param args
	 * @throws NotBoundException
	 * @throws IOException
	 */
	public static void main(String[] args) throws NotBoundException, IOException {
		// check the number of arguments and extract them
		if (args.length != 2) {
			System.out.println("Usage: RmiClient <registryHost> <objectName>");
			System.exit(-1);
		}
		String url = "//" + args[0] + "/" + args[1];
		// set the security manager
		if (System.getSecurityManager() == null) {
			System.setSecurityManager(new java.rmi.RMISecurityManager());
		}
		// create the client and run the test
		RmiClient client = new RmiClient(url);
		runTest(client);
	}

	/**
	 * This method talks to the remote summation service to compute the sum.
	 * @param numbers the numbers to sum up remotely.
	 * @return the remotely computed sum.
	 * @throws RemoteException
	 */
	public int computeSum(Collection<Integer> numbers) throws RemoteException {
		// get a remote session
		ISummationSession session = mSessionFactory.getSession();
		// add up the numbers using the remote service
		int sum=-1;
		for(Integer number: numbers){
			session.add(number);
		}
		// get the sum from the remote service and return it
		sum = session.getSum();
		return sum;
	}

	public static void runTest(RmiClient client) throws IOException {
		System.out.println("Computing...");
		Collection<Integer> numbers1 = new ArrayList<Integer>(6);
		for (int i = 1; i < 6; ++i) {
			numbers1.add(i);
		}
		int result1 = client.computeSum(numbers1);
		System.out.println("Result 1 is " + result1);

		System.out.println("Computing...");
		Collection<Integer> numbers2 = new ArrayList<Integer>(3);
		numbers2.add(5);
		numbers2.add(-3);
		numbers2.add(4);
		int result2 = client.computeSum(numbers2);
		System.out.println("Result 2 is " + result2);

		System.out.println("Computing...");
		Collection<Integer> numbers3 = new ArrayList<Integer>(50);
		for (int i = 0; i < 50; ++i) {
			numbers3.add((int) ((Math.random() * 200) - 100));
		}
		int result3 = client.computeSum(numbers3);
		System.out.println("Result 3 is " + result3);
	}

}
